package file;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Scanner;

public class ScannerTest {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		
		try(
				RandomAccessFile file = new RandomAccessFile("scanner.txt", "rw");
				FileChannel channel = file.getChannel();
				Scanner scanner = new Scanner(channel);
				){
			
			file.close();
			
			System.out.println(channel.position());
			System.out.println(file.getFilePointer());
			scanner.nextLine();
			
			//scanner的读取会影响到FileChannel和RandomAccessFile的position
			System.out.println(scanner.nextLine()); 
			System.out.println(channel.position());
			System.out.println(file.getFilePointer());
			
			file.seek(0);  //设置FileChannel和RandomAccessFile的position不会scanner的读取位置
			System.out.println(channel.position());
			System.out.println(file.getFilePointer());
			System.out.println(scanner.nextLine()); 
		}
	}
}
